import { createApp } from 'vue';
import App from './App.vue';

// 引入路由
import Router from './routers/index';

// 引入Vant
import Vant from 'vant';

// 引入组件样式
import 'vant/lib/index.css';

// 引入cookie
import Cookies from 'vue3-cookies';

// 引入utils
import Utils from './utils/utils';

// 引入Api
import Api from './api/index';

const app = createApp(App)
.use(Router)
.use(Vant)
.use(Cookies)
.use(Utils)
.use(Api)
.mount('#app');

Router.beforeEach(async (to, from, next) => {
    let auth = to.meta.auth ? true : false;

    if(auth === true)
    {
        let business = app.$cookies.get('business');

        if(!business)
        {

            app.$showNotify({
                type:'warning',
                message:'请先登录',
                duration:2000
            })
            
            next('/business/base/login');
            return;
        }

        let id = business.id ?? 0;
        let mobile = business.mobile ?? 0;
        
        let result = await app.$Api.check({id,mobile});

        if(result && result.code === 0)
        {
            app.$showNotify({
                type:'warning',
                message:result.msg,
                duration:2000
            });

            app.$cookies.remove('business');
            
            next('/business/base/login');
            return;
        }

        app.$cookies.set('business',result.data);

        next();

    }else{
        next();
    }
})